package com.cityos.android.mystudynestedscroll.ui.activity

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.widget.Toolbar
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat
import androidx.core.widget.NestedScrollView
import com.cityos.android.mystudynestedscroll.R
import com.cityos.android.mystudynestedscroll.databinding.ActivityScrollerBehaviorBinding

class ScrollerBehaviorActivity : AppCompatActivity() {
    private lateinit var mainBinding: ActivityScrollerBehaviorBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mainBinding = ActivityScrollerBehaviorBinding.inflate(layoutInflater).apply {
            setContentView(this.root)
        }
    }
}

class ScrollerBehavior(context: Context? , attrs: AttributeSet?) : CoordinatorLayout.Behavior<NestedScrollView>(context , attrs) {

    override fun layoutDependsOn(parent: CoordinatorLayout , child: NestedScrollView , dependency: View): Boolean {
        return dependency is LinearLayout && dependency.id == R.id.ll_bar_layout
    }

    override fun onDependentViewChanged(parent: CoordinatorLayout , child: NestedScrollView , dependency: View): Boolean {
        val barLayout = dependency as? LinearLayout ?: return false
        child.translationY = barLayout.top.toFloat()
        return true
    }

    override fun onLayoutChild(parent: CoordinatorLayout , child: NestedScrollView , layoutDirection: Int): Boolean {
        val barLayout = parent.findViewById<View>(R.id.ll_bar_layout)
        val toolbar = parent.findViewById<Toolbar>(R.id.toolbar)
        val height = barLayout.height
        child.layout(0 , height , parent.width , parent.height + height - toolbar.height)
        return true
    }

    override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout , child: NestedScrollView , directTargetChild: View , target: View , axes: Int , type: Int): Boolean {
        return axes == ViewCompat.SCROLL_AXIS_VERTICAL
    }


    override fun onNestedPreScroll(coordinatorLayout: CoordinatorLayout , child: NestedScrollView , target: View , dx: Int , dy: Int , consumed: IntArray , type: Int) {
        Log.e("hailong" , "${child.scrollY}");
        val barLayout = coordinatorLayout.findViewById<LinearLayout>(R.id.ll_bar_layout)
        val toolbar = coordinatorLayout.findViewById<Toolbar>(R.id.toolbar)
        if (dy < 0) {
            if (child.scrollY > 0) {
                consumed[1] = 0
                barLayout.alpha = 0f
                toolbar.alpha = 1f
            } else if (barLayout.bottom <= barLayout.height) {
                var c = dy
                if (barLayout.bottom - c >= barLayout.height) {
                    c = barLayout.bottom - barLayout.height
                }
                consumed[1] = c
                ViewCompat.offsetTopAndBottom(barLayout , -c)
                val a = (barLayout.bottom - toolbar.height) / (barLayout.height - toolbar.height.toFloat())
                barLayout.alpha = a
                toolbar.alpha = 1f - a
            } else {
                consumed[1] = 0
                barLayout.alpha = 0f
                toolbar.alpha = 1f
            }
        } else {
            if (barLayout.bottom <= toolbar.height) {
                consumed[1] = 0
                barLayout.alpha = 0f
                toolbar.alpha = 1f
            } else {
                var c = dy
                if (barLayout.bottom - c <= toolbar.height) {
                    c = barLayout.bottom - toolbar.height
                }
                ViewCompat.offsetTopAndBottom(barLayout , -c)
                consumed[1] = c
                val a = (barLayout.bottom - toolbar.height) / (barLayout.height - toolbar.height.toFloat())
                barLayout.alpha = a
                toolbar.alpha = 1f - a
            }
        }
    }


}
